var adminobj = {
    add:function (id=0) {
        if (id==0){
            window.location.href='/admin/add';
        } else{
            window.location.href='/admin/add?id='+id;
        }
    },

    submit:function (userid=0) {
        var form = $("#from_user");
        var data = {};
        var params={};
        var validFlag = true;
        var up_error = true;
        var arr = ["jpg","png","gif","jpeg","apk","mobileconfig"];
        $.each(form.find('input'), function (i, v) {
            var type = $(v).attr('type');
            switch (type) {
                case 'FILE':
                case 'file':
                    var index = v.value.lastIndexOf('.');
                    var ext = v.value.substr(index+1);
                    if (arr.includes(ext)){
                            var fileObj = v.files[0];
                            if (typeof (fileObj) == "undefined" || fileObj.size>=9871536) {
                                //这里是我自己定义的弹框方法
                                layer.msg('请上传正确的文件或者确保文件小于10M！');
                                validFlag = false;
                                return;
                            }
                            // var formFile = new FormData();
                            // //加入文件对象,向接口传入两个参数file,id
                            // formFile.append(v.name, fileObj);
                            data[v.name] = fileObj;
                    } else if(userid==0){
                        layer.msg( $(v).attr('title')+'上传错误');
                        validFlag = false;
                        return;
                    }
                    break;
                default:
                    val = $.trim(v.value);
                    if (!val) {
                        layer.msg('都是必填项，不能微空！');
                        validFlag = false;
                        return false;
                    }
                    params[v.name] = val;
                    break;
            }
        });
        if (!validFlag) return false;
        var formFile = new FormData();
       $.each(data,function (i,v) {
           formFile.append(i, v);
       })
        if (userid!=0){
            formFile.append('id', JSON.stringify(userid));
        }
       var datas = formFile;
        $.ajax({
            url:'/admin/add-from',
            data: datas,
            type: "POST",
            dataType: "json",
            //上传文件无需缓存
            cache: false,
            //用于对data参数进行序列化处理 这里必须false
            processData: false,
            //必须
            contentType: false,
            success: function (res) {
                if(res.code !== 200){
                    up_error = false;
                }
            },
        });

        var csrfToken = $('meta[name="csrf-token"]').attr("content");
        params['_csrf'] = csrfToken;
        $.post("/admin/add-from", params, function (json) {
            if (json.code==1){
                layer.msg(json.msg);
                window.location.href="/admin/user";
                return;
            }else{
                layer.msg(json.msg);
                window.location.href="/admin/user";
                return;
            }
        },'json');
    },

    pass:function(){
        var pass = $("#from_pass");
        var parma = {};
        var validFlag = true;
      $.each(pass.find('input'),function (i,v) {
          if (v.value!=''){
              parma[v.name] = v.value;
          }else {
              layer.msg('必填项不能为空！');
              return validFlag=false;
          }
      });
        if (!validFlag) return false;
        var csrfToken = $('meta[name="csrf-token"]').attr("content");
        parma['_csrf'] = csrfToken;
      $.post('/admin/password',parma,function (json) {
          if (json.code==1){
              layer.msg(json.mag);
              window.location.href='/admin/index';
              return;
          }else {
              layer.msg(json.mag);
              window.location.href='/admin/pass';
              return;
          }

      },'json')
    },

    renew:function (e,id) {
        if(e.value!==''){
            var time = e.value;
            $.post("/admin/renew", {'time':time,'id':id}, function (json) {
                if (json.code==1){
                    location.reload();
                    layer.msg('更新成功');
                } else {
                    layer.msg('更新时间失败');
                }
            },'json');
        }
    },

    can:function (id) {
        $.post("/admin/can", {'id':id}, function (json) {
            if (json.code=='1'){
                var str = "<div class='dialogModal_content'>\n" +
                    "<span style='padding: 5px;'>App地址：</span><input type='text' class='form-control' id='app' value="+json.app+" />\n"+
                    "<span style='padding: 5px;'>Ios地址：</span><input type='text' class='form-control' id='ios' value="+json.ios+" />\n"+
                    "</div>";
                //页面层

                layer.open({
                    type: 1,
                    skin: 'layui-layer-rim', //加上边框
                    area: ['420px', '240px'], //宽高
                    content: str
                });
            }else {
                layer.msg('查看失败！');
            }
        },'json');
    },

    switchs:function (id) {
        $.post('/admin/switchs',{'id':id},function (e) {
            if (e.code=='1'){
                location.reload();
            }else if (e.code=='0') {
                layer.msg(e.msg);
            }
        },'json')
    }
}